1704B - Luke is a Foodie - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

import os, sys
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import *
from collections import defaultdict, deque, Counter
import math, string
from heapq import *
from operator import add
from itertools import accumulate

BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        import os

        self.os = os
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            self.os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")


inf = float("inf")
en = lambda x: list(enumerate(x))
ceil_ = lambda a, b: (a + b - 1) // b


ii = lambda: int(input())
r = lambda: map(int, input().split())
rr = lambda: list(r())


def solve():
    n , x = r()
    arr = rr()

    brr = []

    for i in arr:
        brr += sorted([-x + i, x + i]),
    
    x = [-inf, inf]
    ans = 0
    
    for i,j in brr:
        if not (i > x[1]) and not (j < x[0]):
            x[0] = max(x[0],i)
            x[1] = min(x[1],j)
        else:
            x = [i,j]
            ans += 1
        
        x = sorted(x)
    
    print(ans)

for i in range(1, ii() + 1):
    solve()

C++ Code:

#include <iostream>
int main()
{
    int t;
    std::cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        std::cin>>n;
        int x;
        std::cin>>x;
        int a[n];
        for(int j=0;j<n;j++)
        {
            std::cin>>a[j];
        }
        int minNoChange = 0;
        int min = a[0];
        int max = a[0];
        for(int j=1;j<n;j++)
        {
            if(a[j]<min)
            {
                min = a[j];
            }
            if(a[j]>max)
            {
                max = a[j];
            }
            if (max%2==0 && min%2==0 || max%2==1 && min%2==1)
            {
                if(((max-min)/2)>x)
                {
                    min = a[j];
                    max = a[j];
                    minNoChange++;
                }
            }
            else
            {
                if(max%2==0)
                {
                    if(((max-(min-1))/2)>x)
                    {
                        min = a[j];
                        max = a[j];
                        minNoChange++;
                    }
                }
                else
                {
                    if((((max+1)-min)/2)>x)
                    {
                        min = a[j];
                        max = a[j];
                        minNoChange++;
                    }
                }
            }
        }
        std::cout<<minNoChange<<std::endl;
    }
}


Comments

Submit
0 Comments
More Questions

1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers